﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.ComponentModel;

using Developmentalmadness.Web.UI.AjaxAbstractions;

namespace Developmentalmadness.Web.UI.ExtJsControlLibrary
{
    public class ExtJsButton : ExtJsComponent
    {
        private List<ScriptReference> _references = new List<ScriptReference>();

        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.Button";

            String[] properties = new String[]{
                "allowDepress", "clickEvent", "disabled", "enableToggle", "handleMouseEvents",
                "handler", "hidden", "icon", "iconCls", "menuAlign", "minWidth",
                "pressed", "repeat", "scope", "tabIndex", "template", "text", "toggleGroup",
                "toggleHandler", "tooltip", "tooltipType", "type"
            };

            Type component = typeof(ExtJsButton);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtJsButton.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            // menu
            if (menu != null)
            {
                // items script
                ScriptControlDescriptorBase descriptor = (ScriptControlDescriptorBase)menu.GetScriptDescriptors()[0];

                // get the items's list of script dependencies
                _references.AddRange(menu.GetScriptReferences());

                // write config script to properties
                desc.AddPropertyBase("menu", descriptor.WriteScript(ScriptType.ConfigOnly));
            }

            return list;
        }

        #region Public Properties

        public Boolean allowDepress
        {
            get;
            set;
        }

        public String clickEvent { get; set; }

        public Boolean? disabled { get; set; }

        public Boolean? enableToggle { get; set; }

        public Boolean? handleMouseEvents { get; set; }

        public String handler { get; set; }

        public Boolean? hidden { get; set; }

        public String icon { get; set; }

        public String iconCls { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ExtJsMenu menu { get; set; }

        public String menuAlign { get; set; }

        public Unit? minWidth { get; set; }

        public Boolean? pressed { get; set; }

        public Boolean? repeat { get; set; }

        public String scope { get; set; }

        public Int32? tabIndex { get; set; }

        public String template { get; set; }

        public String text { get; set; }

        public String toggleGroup { get; set; }

        public String toggleHandler { get; set; }

        public String tooltip { get; set; }

        public String tooltipType { get; set; }

        public String type { get; set; }
        #endregion
    }
}
